//
//  JNNavigationController.swift
//  SinaWeibo
//
//  Created by 9264 on 16/9/19.
//  Copyright © 2016年 LY9264. All rights reserved.
//

import UIKit

class JNNavigationViewController: UINavigationController, UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        

        print(viewControllers.count)

        if viewControllers.count > 0 {

            if viewControllers.count == 1 {

                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))

            } else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))

            }
            
            viewController.title = "第\(viewControllers.count + 1)级控制器"
            
        }

        super.pushViewController(viewController, animated: animated)
        
    }

    func backAction()  {
        
        popViewController(animated: true)

    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            return false
        }

        return true
    }

}
